package com.hc.server.actions;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.db.DBFactory;
import com.hc.server.db.Mporder;

/**
 * 获取用户的订单记录
 * @author xujh
 *
 */
public class QUERY_ORDERS implements IAction {

	private static final String USER_ID = "USER_ID";
	private static final String PAGE_INDEX = "PAGE_INDEX";
	private static final String PAGE_SIZE = "PAGE_SIZE";

	private static final String TOTAL_RESULT = "TOTAL_RESULT";
	private static final String ORDERS = "ORDERS";
	
	private static final String ORDER_ID = "OID";
//	private static final String ORDER_DES = "DES";
	private static final String ORDER_STATUS = "STATUS";
	private static final String ORDER_AMOUNT = "AMOUNT";
	private static final String ORDER_CREATE_TIME = "CREATE_TIME";
//	private static final String ORDER_REMARK = "REMARK";
	
	@SuppressWarnings("unchecked")
	@Override
	public void execute(Request req, Response resp) {
		String userID;
		int pageIndex;
		int pageSize;
		try {
			userID = req.getStrArg(USER_ID);
			pageIndex =  req.getIntArg(PAGE_INDEX);
			pageSize = req.getIntArg(PAGE_SIZE); 
		} catch (JSONException e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD, Error.INVALID_ARGS_ERR_MSG);
			return;
		}
		
		Session session = DBFactory.openSession();
		Transaction t = session.beginTransaction();
		t.begin();
		String totalHql = "from Mporder as mpo where mpo.user = :UID";
		Query totalQuery = session.createQuery(totalHql);
		totalQuery.setParameter("UID", userID);
		List<Mporder> allOrders = totalQuery.list();
		int totalResult = allOrders.size();
		JSONArray rets = new JSONArray();
		if(0 != totalResult){
			String hql = "from Mporder as mpo where mpo.user = :UID order by mpo.createTime desc";
			Query query = session.createQuery(hql);
			query.setParameter("UID", userID);
			query.setFirstResult((pageIndex - 1)*pageSize + 1);
			query.setMaxResults(pageSize);
			Iterator<Mporder> mpoIter = query.iterate();
			while(mpoIter.hasNext()){
				Mporder mpo = mpoIter.next();
				JSONObject ret = new JSONObject();
				try{
					ret.put(ORDER_ID, mpo.getId());
					ret.put(ORDER_AMOUNT, mpo.getAmount());
					ret.put(ORDER_STATUS, mpo.getStatus());
					//ret.put(ORDER_DES, mpo.getDes());
					ret.put(ORDER_CREATE_TIME, mpo.getCreateTime());
					//ret.put(ORDER_REMARK, mpo.getRemark());
				}catch (Exception e) {
					e.printStackTrace();
					resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
					return;
				}
				rets.put(ret);
			}
			session.close();
		}
		resp.appendRespContent(ORDERS, rets);
		resp.appendRespContent(TOTAL_RESULT, totalResult);
	}

}
